package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


/**
 * 对外公共接口调用类
 */
@Controller
@RequestMapping("/public")
public class PublicController extends BaseController {

    @Resource
    private DeptService deptService;
    @Resource
    private EmpService empService;
    @Resource
    private AreaService areaService;
    @Resource
    private BtyGufenzhuanrangService btyGufenzhuanrangService;
    @Autowired
    private BtyWageService btyWageService;
    @Autowired
    private BtyYhkbgService btyYhkbgService;


    @RequestMapping("/syncDeptFromDD")
    @ResponseBody
    public ResponseData syncDeptFromDD() throws Exception {
        ResponseData r = deptService.syncFromDingDing();
        return r;
    }

    @RequestMapping("/syncUserFromDingDing")
    @ResponseBody
    public ResponseData syncUserFromDingDing() throws Exception {
        deptService.syncUserFromDingDing();
        ResponseData r = ResponseData.success("已执行");
        return r;
    }

    @RequestMapping("/syncUserFromDDForApp")
    @ResponseBody
    public ResponseData syncUserFromDDForApp(Dept dept) throws Exception {
        if(ToolUtil.isEmpty(dept.getYaoma())){
            return ResponseData.error("缺少参数");
        }
        QueryWrapper q=new QueryWrapper();
        q.eq("description",dept.getYaoma());
        Dept curDept= this.deptService.getOne(q);
        if(ToolUtil.isEmpty(curDept)){
            return ResponseData.error("根据参数未获取到部门信息");
        }
        int size = deptService.syncUserFromDDForApp(curDept);
        ResponseData r = SuccessResponseData.success("同步"+size+"人");
        deptService.tongbuYonghu(dept.getYaoma());
        return r;
    }

    @RequestMapping("/queryAreas")
    @ResponseBody
    public ResponseData queryAreas() throws Exception {
        return areaService.queryAreas();
    }

    @RequestMapping("/shengriTuisong")
    @ResponseBody
    public ResponseData shengriTuisong() throws Exception {
        empService.shengriTuisong();
        ResponseData r = SuccessResponseData.success("生日推送发送成功");
        return r;
    }

    @RequestMapping("/queryAllDaiBanQty")
    @ResponseBody
    public ResponseData queryAllDaiBanQty(String account,String name) throws Exception {

        String userInfor=  (String)this.getHttpServletRequest().getAttribute("userInfor");
        System.out.println("userInfor="+userInfor);
        ResponseData r = SuccessResponseData.success();
        JSONArray dataArr = new JSONArray();
        {
            JSONObject d = new JSONObject();
            d.put("id","250");
            d.put("title","工资审批");
            d.put("daiban_qty",btyWageService.queryAllDaibanQty(account));
            dataArr.add(d);
        }
        {
            JSONObject d = new JSONObject();
            d.put("id","264");
            d.put("title","股份转让");
            d.put("daiban_qty",btyGufenzhuanrangService.queryAllDaibanQty(account));
            dataArr.add(d);
        }
        {
            JSONObject d = new JSONObject();
            d.put("id","262");
            d.put("title","银行卡审批");
            d.put("daiban_qty",btyYhkbgService.queryAllDaibanQty(account));
            dataArr.add(d);
        }
        r.setData(dataArr);
        return r;
    }


}
